import { getCarList } from '@/api/codeServer'
import { Modal, type ModalProps } from 'antd'
import { YmTable } from 'ym-design'

export default function CarModal(
  props: ModalProps & { entId?: string; associationId?: string }
) {
  const { entId, associationId, ...rest } = props

  return (
    <Modal
      title="已备案车辆"
      centered
      destroyOnClose
      width={850}
      {...rest}
      styles={{
        body: {
          height: 480
        }
      }}
    >
      <YmTable
        toolBar={false}
        searchForm={{
          formItems: [
            {
              label: '车牌号',
              name: 'carBrand',
              valueType: {
                type: 'input',
                inputProps: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              label: '运力类型',
              name: 'coopType',
              valueType: {
                type: 'select',
                selectProps: {
                  placeholder: '请输入',
                  allowClear: true,
                  options: [
                    { label: '企业车辆', value: '51' },
                    { label: '外协车辆', value: '52' }
                  ]
                }
              }
            }
          ]
        }}
        columns={[
          {
            title: '车牌号',
            dataIndex: 'carBrand',
            key: 'carBrand',
            width: 80
          },
          {
            title: '车辆类型',
            dataIndex: 'carType',
            key: 'carType',
            width: 120
          },
          {
            title: '车牌颜色',
            dataIndex: 'plateColor',
            key: 'plateColor',
            width: 80
          },
          {
            title: '运力类型',
            dataIndex: 'coopType',
            key: 'coopType',
            width: 80
          },
          {
            title: '备案时间',
            dataIndex: 'dateTime',
            key: 'dateTime',
            width: 120
          }
        ]}
        request={async ({ page }, formValue) => {
          const { carBrand, coopType } = formValue as any
          const { pageIndex = 1, pageSize = 10 } = page ?? {}
          if (associationId) {
            const { list, total } = await getCarList({
              associationId,
              carBrand,
              pageIndex,
              pageSize,
              coopType,
              entId
            })
            return {
              list,
              total
            }
          }
          return {
            list: [],
            total: 0
          }
        }}
      />
    </Modal>
  )
}
